﻿Imports tSvcControl
Imports System.Configuration

Public Class Form1

    Private terrariaProcess As tServer
    Private terrariaProcessCol As tServerCollection

    Private Sub btnStartServer_Click(sender As Object, e As EventArgs) Handles btnStartServer.Click
        If IsNothing(terrariaProcess) Then
            terrariaProcess = New tServer()
            Dim terrariaConfiguration As New tConfiguration
            Dim singleServerConfig As TerrariaServerConfigurationElement

            terrariaConfiguration.ReadFromConfig()
            Me.tmrAutosave.Interval = terrariaConfiguration.AutoSave.IntervalInMilliseconds
            Me.tmrAutosave.Enabled = terrariaConfiguration.AutoSave.Enabled

            'buffer information is not held in config file and must be set manually
            singleServerConfig = terrariaConfiguration.Servers(0)
            singleServerConfig.BufferConsole = True
            singleServerConfig.BufferErrors = True

            Dim ret As Int32 = terrariaProcess.StartServer(singleServerConfig)
            tmrCheckBuffers.Start()

            'send TShock commands if server is TShock with 0.25s delay
            If terrariaProcess.IsTShock Then
                Threading.Thread.Sleep(250)
                terrariaProcess.SelectTShockWorld()
                Threading.Thread.Sleep(250)
                terrariaProcess.SelectTShockPort()
            End If

        End If
    End Sub

    Private Sub btnSendCommand_Click(sender As Object, e As EventArgs) Handles btnSendCommand.Click
        Try
            terrariaProcess.SendCommand(Me.txtCommand.Text)
        Catch ex As Exception
            Me.txtStdErr.Text = String.Concat(Me.txtStdErr.Text, ex.ToString, Environment.NewLine)
        End Try
    End Sub

    Private Sub ErrorEvent()
        'Me.txtStdErr.Text = String.Concat(Me.txtStdErr.Text, xblah.ReadErrorBuffer)
    End Sub

    Private Sub OutputEvent()
        'Me.txtStdOut.Text = String.Concat(Me.txtStdOut.Text, xblah.ReadOutputBuffer)
    End Sub

    Private Sub UpdateStdErr()
        Me.txtStdErr.Text = String.Concat(Me.txtStdErr.Text, terrariaProcess.ReadErrorBuffer)
        Me.txtStdErr.SelectionStart = Me.txtStdErr.Text.Length
        Me.txtStdErr.ScrollToCaret()
    End Sub

    Private Sub UpdateStdOut()
        Me.txtStdOut.Text = String.Concat(Me.txtStdOut.Text, terrariaProcess.ReadOutputBuffer)
        Me.txtStdOut.SelectionStart = Me.txtStdOut.Text.Length
        Me.txtStdOut.ScrollToCaret()
    End Sub

    Private Sub tmrCheckBuffers_Tick(sender As System.Object, e As System.EventArgs) Handles tmrCheckBuffers.Tick
        UpdateStdOut()
        UpdateStdErr()
    End Sub

    Private Sub btnStopServer_Click(sender As System.Object, e As System.EventArgs) Handles btnStopServer.Click
        Me.Cursor = Cursors.WaitCursor

        terrariaProcess.Broadcast("shuttign down serer in... 5")
        Threading.Thread.Sleep(1000)
        terrariaProcess.Broadcast("shuttign down serer in... 4")
        Threading.Thread.Sleep(1000)
        terrariaProcess.Broadcast("shuttign down serer in... 3")
        Threading.Thread.Sleep(1000)
        terrariaProcess.Broadcast("shuttign down serer in... 2")
        Threading.Thread.Sleep(1000)
        terrariaProcess.Broadcast("shuttign down serer in... 1")
        Threading.Thread.Sleep(1000)

        If terrariaProcess.IsServerRunning Then
            'stop auto-update of stdout
            tmrCheckBuffers.Enabled = False

            'forece read of buffers
            UpdateStdOut()
            UpdateStdErr()

            'stop server
            terrariaProcess.StopServer()

            'forece read of buffers
            UpdateStdOut()
            UpdateStdErr()

        End If

        Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub tmrAutosave_Tick(sender As Object, e As EventArgs) Handles tmrAutosave.Tick
        Try
            terrariaProcess.AutoSave()
        Catch ex As Exception
            Me.txtStdErr.Text = String.Concat(Me.txtStdErr.Text, ex.ToString, Environment.NewLine)
        End Try
    End Sub

    Private Sub btnStartAll_Click(sender As Object, e As EventArgs) Handles btnStartAll.Click
        Dim terrariaConfiguration As New tConfiguration
        terrariaProcessCol = New tServerCollection

        AddHandler terrariaProcessCol.LogEvent, AddressOf logCollectionEvents

        terrariaConfiguration.ReadFromConfig()
        Me.txtStdOut.Text = String.Concat(Me.txtStdOut.Text, Environment.NewLine, "Config read!")

        terrariaProcessCol.StartAllServers(terrariaConfiguration)
        Me.txtStdOut.Text = String.Concat(Me.txtStdOut.Text, Environment.NewLine, "All servers started!")

    End Sub

    Private Sub logCollectionEvents(ByVal serverName As String, ByVal eventMessage As String, ByVal innerException As Exception, ByVal eventType As EventLogEntryType)

        Me.txtStdErr.Text = String.Concat(Me.txtStdErr.Text, serverName, " - ", eventMessage, " - ", eventType.ToString, Environment.NewLine, innerException.ToString)

    End Sub

    Private Sub btnStopAll_Click(sender As Object, e As EventArgs) Handles btnStopAll.Click
        terrariaProcessCol.StopAllServers()
        Me.txtStdOut.Text = String.Concat(Me.txtStdOut.Text, Environment.NewLine, "All servers stopped!")
    End Sub
End Class
